package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService facilityService;
    @Autowired
    private ApartmentLabelService labelService;
    @Autowired
    private ApartmentFeeValueService feeValueService;

    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

     @Autowired
     private  ApartmentInfoMapper apartmentInfoMapper;


    /**
     * 入库:
     *      1.图片信息 graph
     *      2.公寓配套
     *      3.公寓标签
     *      4.公寓杂费
     *  原则: 多对多关系中 中间表的修改操作 一般都是删除处理. 根据主表id删除.
     * @param apartmentSubmitVo
     */
    @Override
    @Transactional
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //检查是插入还是修改
        if(apartmentSubmitVo.getId() != null){
            //公寓id号
            long id = apartmentSubmitVo.getId();
            //修改 先删除中间表数据
            //1.删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoQuery = new LambdaQueryWrapper<>();
            graphInfoQuery.eq(GraphInfo::getItemId,id)
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoQuery);

            //2.删除公寓配套
            LambdaQueryWrapper<ApartmentFacility> facilityQuery = new LambdaQueryWrapper<>();
            facilityQuery.eq(ApartmentFacility::getApartmentId,id);
            facilityService.remove(facilityQuery);

            //3.删除标签
            LambdaQueryWrapper<ApartmentLabel> labelQuery = new LambdaQueryWrapper<>();
            labelQuery.eq(ApartmentLabel::getApartmentId,id);
            labelService.remove(labelQuery);

            //4.删除杂费
            LambdaQueryWrapper<ApartmentFeeValue> feeValueQuery = new LambdaQueryWrapper<>();
            feeValueQuery.eq(ApartmentFeeValue::getApartmentId,id);
            feeValueService.remove(feeValueQuery);
        }

        //代码补充 前端值传递了 省市县的id号 应该查询数据的名称之后入库
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //先将公寓入库  如果是主键自增 则自动回显数据
        this.saveOrUpdate(apartmentSubmitVo);
        //回显id
        long apartmentId = apartmentSubmitVo.getId();


        //新增图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            //将用户的数据vo 转化为可以入库的info对象
            graphVoList.forEach(graphVo -> {
                GraphInfo info = new GraphInfo();
                info.setName(graphVo.getName());
                info.setUrl(graphVo.getUrl());
                info.setItemType(ItemType.APARTMENT);
                //入库公寓id
                info.setItemId(apartmentId);
                graphInfoList.add(info);
            });
            graphInfoService.saveBatch(graphInfoList);
        }

        //2.新增配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> facilityList = new ArrayList<>(facilityInfoIds.size());
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            facilityInfoIds.forEach(facilityInfoId -> {
                //ApartmentFacility info = ApartmentFacility.builder().facilityId(facilityInfoId).apartmentId(apartmentId).build();
                ApartmentFacility info = new ApartmentFacility();
                info.setFacilityId(facilityInfoId).setApartmentId(apartmentId);
                facilityList.add(info);
            });
            //入库操作
            facilityService.saveBatch(facilityList);
        }

        //3.新增标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> labelList = new ArrayList<>(labelIds.size());
        if(!CollectionUtils.isEmpty(labelIds)){
            labelIds.forEach(labelId -> {
                ApartmentLabel apartmentLabel = ApartmentLabel
                        .builder()
                        .labelId(labelId)
                        .apartmentId(apartmentId)
                        .build();
                labelList.add(apartmentLabel);
            });
            //入库操作
            labelService.saveBatch(labelList);
        }

        //4.新增标签信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> feeValues = new ArrayList<>(feeValueIds.size());
        if(!CollectionUtils.isEmpty(feeValueIds)){
            feeValueIds.forEach(feeValueId -> {
                ApartmentFeeValue feeValue = ApartmentFeeValue
                        .builder()
                        .feeValueId(feeValueId)
                        .apartmentId(apartmentId).build();
                feeValues.add(feeValue);
            });
            //入库操作
            feeValueService.saveBatch(feeValues);
        }
    }

    /**
     * 实现方式:
     *      1.纯对象写法 全部采用MP的操作  效率低
     *      2.Sql的方式  效率高  逻辑相对复杂
     *
     * mp分页操作-自定义分页!!!!
     */
    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        page = apartmentInfoMapper.pageItem(page,queryVo);
        return page;
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        return apartmentInfoMapper.getDetailById(id);
    }

    @Override
    @Transactional
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemType,1);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        facilityService.remove(facilityLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        labelService.remove(labelLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        feeValueService.remove(feeValueLambdaQueryWrapper);

        this.removeById(id);
    }


}








